package uplift.project;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

public class SuggestionActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.suggestion);
        
        Intent startIntent = this.getIntent();
        Bundle extras = startIntent.getExtras();
        String suggestionName = extras.getString("name");
        String suggestionDesc = extras.getString("desc");
        int imageRes = extras.getInt("image");
        double rating = extras.getDouble("rating");

        //TextView suggName = (TextView) this.findViewById(R.id.suggestion_name);
    	ImageView image = (ImageView) this.findViewById(R.id.suggestion_image);
    	TextView description = (TextView) this.findViewById(R.id.suggestion_description);
    	final ListView feedback = (ListView) this.findViewById(R.id.feedback_list);
    	RatingBar ratingBar = (RatingBar) this.findViewById(R.id.ratingBar1);
    	TextView ratingText = (TextView) this.findViewById(R.id.ratingText);
        
    	String[] userFeedback = new String[]{"It was awesome!", "They really enjoyed it.", "10/10 would do again"};
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.feedback_list_item, R.id.list_content, userFeedback);
    	feedback.setAdapter(adapter);
    	feedback.postDelayed(new Runnable() {
    		public void run() {
    			setListViewHeightBasedOnChildren(feedback);
    		}
    	}, 200);
        
        //suggName.setText(suggestionName);
        image.setImageResource(imageRes);
        description.setText(suggestionDesc);
        ratingBar.setRating((float) rating);
        
        ratingText.setText(rating + "/5 from 20 users");
        
        // show title as the name of the suggestion
        this.setTitle(suggestionName);
		// set the action bar to have up-navigation enabled
	    ActionBar actionBar = getActionBar();
	    actionBar.setDisplayHomeAsUpEnabled(true);
    }
    
    
    // called when the "I'm finished" button is clicked.  
    // transitions to the rating screen
    public void finished(View v) {
    	Intent i = new Intent(v.getContext(), RatingActivity.class);
    	startActivity(i);
    }
    

    // handles action item button presses
 	@Override
 	public boolean onOptionsItemSelected(MenuItem item) {
 		if(item.getItemId() == android.R.id.home) {
 			// move up the hierarchy

 			Intent i = new Intent(getApplicationContext(), SuggestionListActivity.class);
 			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 			startActivity(i);
 		} else if(item.getItemId() == R.id.menu_home) {
 			// go to Home screen
 			
 			Intent i = new Intent(getApplicationContext(), UpliftActivity.class);
 			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 			startActivity(i);
 		} else if(item.getItemId() == R.id.menu_my_events) {
 			// go to My Events screen
 			
 			Intent i = new Intent(getApplicationContext(), MyEventsTabActivity.class);
 			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 			startActivity(i);
 		}
 		return true;
 	}
 	
 	// set up Action Bar
 	@Override
 	public boolean onCreateOptionsMenu(Menu menu) {
 	    MenuInflater inflater = getMenuInflater();
 	    inflater.inflate(R.menu.action_bar, menu);
 	    return true;
 	}
	
	// hack to make list of feedback appear correctly
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
}
